package com.itheima.service.Impl;

import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.MemberSetmealDto;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private RedisTemplate redisTemplate;

    //登录
    @Override
    public void login(MemberSetmealDto memberSetmealDto) {

        //获取缓存种的验证码
        String code = (String) redisTemplate.opsForValue().get(memberSetmealDto.getTelephone());

        //判断验证码是否正确
        if (code != null && code.equals(memberSetmealDto.getValidateCode())) {
            //验证码正确，登录成功，删除缓存中的验证码
//            redisTemplate.delete(memberSetmealDto.getTelephone());

            //判断当前用户是否为会员，如果是会员，直接返回，如果不是会员，自动完成注册
            Member member = memberDao.findByphoneNumber(memberSetmealDto.getTelephone());
            if (member == null) {

                Member member1 = new Member();
                member1.setPhoneNumber(memberSetmealDto.getTelephone());

                memberDao.insert(member1);
            }
        }
    }
}
